#pragma once

#ifndef _CORE_REDIRECTED_IO_H_
#define _CORE_REDIRECTED_IO_H_

#include "Poco\Runnable.h"
#include "Poco\Event.h"
#include "Poco\SharedPtr.h"

#include "Core\SafeMutex.h"

#include <sstream>
#include <vector>

namespace ExamSystem
{
    using Poco::Runnable;
    using Poco::Event;
    using Poco::SharedPtr;

    class CRedirectedIO : Runnable
    {
    protected:
        CRedirectedIO(void);
    public:
        ~CRedirectedIO(void);

    public:
        virtual void run();

    public:
        void StartThread(void);
        void StopThread(void);

        static CRedirectedIO &Instance(void);

#ifdef _UNICODE
        int GetLines(std::vector<std::wstring> &lines);
#else
        int GetLines(std::vector<std::string> &lines);
#endif

    protected:
        void RedirectStdIO(void);
        void ParseStdOut(void);

    protected:
        HANDLE m_hRead;
        HANDLE m_hWrite;

        std::stringstream m_strStream;
        CSafeMutex m_lockStrStream;
        Event m_synThreadStopped;
        Event m_synIORedirected;

        // the only one instance
        static SharedPtr<CRedirectedIO> ms_instance;
    };

}
#endif